define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Promotionsdetail, $stateParams, $window, $timeout, $ionicSlideBoxDelegate,GoPage) {
		$scope.isLoad = false; //可以加载
		$scope.act_id = $stateParams.act_id;
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.goods_list = new Object();
		$scope.datas = new Object();
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var all = $window.innerHeight;
			//计算滚动条
			$scope.myObj = {
				"height": all - 44 + "px"
			};
			$scope.init_data();
		}, false);
		//滑动时候触发
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			//手指滑动
			$scope.selectTab = index;
		};
		
		//加载数据
		$scope.init_data = function() {
			//获取轮播图
//			Promotionsdetail.getbannerlist(function(data){
//				$scope.advs = data.associate;
//				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
//				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
//			});
//			//获取详细数据
//			Promotionsdetail.get_detail($scope.act_id, function(data) {
//				$scope.detailData = data;
//			});
			Promotionsdetail.promotion_detail($scope.act_id,function(data){
				$scope.datas = data;
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
			});
			Promotionsdetail.goods_list(1,$scope.act_id,function(data){
				
				if(data.data.length>0)
				$scope.goods_list = data;
				else
				$scope.goods_list = new Object();
				$scope.input.nPage = 1;
				
				console.log($scope.goods_list);
			})
		}
		$scope.init_data();

		//轮播小圆点切换
		$scope.advsgo = function(index) {
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').slide(index);
		}

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Promotionsdetail.promotion_detail($scope.act_id,function(data){
				if(data!=null)
				$scope.datas = data;
				else
				$scope.datas = new Object();
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
			});
			Promotionsdetail.goods_list(1,$scope.act_id,function(data){
				if(data.data.length>0)
				$scope.goods_list = data;
				else
				$scope.goods_list = new Object();
				$scope.input.nPage = 1;
				
				console.log($scope.goods_list);
			},function(){
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1000);
			})
			
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Promotionsdetail.goods_list($scope.input.nPage+1,$scope.act_id,function(data){
				if(data.data.length>0){
					if($scope.goods_list.data!=undefined){
						$scope.goods_list.data = $scope.goods_list.data.concat(data.data);
						$scope.input.nPage += 1;
					}else{
						$scope.goods_list = data;
						$scope.input.nPage = 1;
					}
				}
				
				console.log($scope.goods_list);
			},function(){
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
				$scope.isLoad = false;
			}, 1500);
			})
			
		};
		
	}

	controller.$inject = ['$scope', 'Promotionsdetail', '$stateParams', '$window', '$timeout', '$ionicSlideBoxDelegate','GoPage'];

	return app.registerController("promotionsdetailctrl",controller);
});